﻿namespace Smp_NetServer
{
      
    // 定义接口
    public interface ILoopState
    {
        bool IsEnabled { get; set; }
    }

    // 实现接口的服务类
    public class LoopState : ILoopState
    {
        public bool IsEnabled { get; set; }
    }



    // 修改 LoopService 以依赖接口
    public class LoopService : BackgroundService
    {
        private readonly ILoopState _state;
        public LoopService(ILoopState state) => _state = state;

        protected override async Task ExecuteAsync(CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                if (_state.IsEnabled)
                {
                
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM_dd_HH:mm:ss") + $"\t 执行线程: {Thread.CurrentThread.ManagedThreadId}\t\t" + Environment.CurrentManagedThreadId + "\t循环任务正在执行...");

                }
                await Task.Delay(8000, token);

            }
        }
    }
}
